{# SPDX-License-Identifier: Apache-2.0 -#}

{% extends "admin/base.html" %}

{% import "admin/utils/pagination.html" as pagination %}

{% block title %}Release {{ release.version }} of {{ release.project.name }}{% endblock %}

{% block breadcrumb %}
  <li class="breadcrumb-item"><a href="{{ request.route_path('admin.project.list') }}">Projects</a></li>
  <li class="breadcrumb-item"><a href="{{ request.route_path('admin.project.detail', project_name=release.project.normalized_name) }}">{{ release.project.name }}</a></li>
  <li class="breadcrumb-item active">Release {{ release.version }}</li>
{% endblock %}

{% block content %}
  <div class="card">
    <div class="card-header">
      <h3>Details</h3>
    </div>
    <div class="card-body table-responsive">
      <table class="table table-hover">
        <tr>
          <td>Canonical URL:</td>
          {% set url = request.route_url('packaging.release', name=release.project.normalized_name, version=release.version) %}
          <td><a href="{{ url }}">{{ url }}</a></td>
        </tr>
        <tr>
          <td>Created</td>
          <td>{{ release.created }}</td>
        </tr>
        <tr>
          <td>Uploader</td>
          <td>
            {% if release.uploader %}
            <a href="{{ request.route_path('admin.user.detail', username=release.uploader.username) }}">{{ release.uploader.username }}</a>
            {% endif %}
          </td>
        </tr>
        <tr>
          <td>Created via</td>
          <td><code>{{ release.uploaded_via }}</code></td>
        </tr>
        <tr>
          <td>Trusted Published?</td>
          <td>
            {% if release.trusted_published %}
              <i class="fa fa-fw fa-check text-green"></i>
            {% else %}
              <i class="fa fa-fw fa-times text-red"></i>
              <span>
                <i class="fa fa-info-circle" data-bs-toggle="tooltip" title="All Files of a Release must be TP for this to be True"></i>
              </span>
            {% endif %}
        </tr>
        <tr>
          <td>Yanked</td>
          <td>{{ release.yanked }}</td>
        </tr>
      </table>
    </div>
  </div>
  <div class="card">
    <div class="card-header">
      <h3>Files</h3>
    </div>
    <div class="card-body table-responsive">
      <table class="table table-hover table-striped">
        <thead>
        <tr>
          <th>Filename</th>
          <th>Package Type</th>
          <th>Python Version</th>
          <th>Uploaded via</th>
          <th>Trusted Publisher?</th>
          <th>Inspector</th>
        </tr>
        </thead>
        <tbody>
        {% for file in release.files %}
        <tr>
          <td><a href="{{ request.route_url('packaging.file', path=file.path) }}">{{ file.filename }}</a></td>
          <td>{{ file.packagetype }}</td>
          <td>{{ file.python_version }}</td>
          <td><code>{{ file.uploaded_via }}</code></td>
          <td>
            {% if file.uploaded_via_trusted_publisher %}
              <i class="fa fa-fw fa-check text-green"></i>
            {% else %}
              <i class="fa fa-fw fa-times text-red"></i>
            {% endif %}
          </td>
          <td>
            <a href="https://inspector.pypi.io/project/{{ release.project.normalized_name }}/{{ release.version }}/packages/{{ file.path }}/">🕵️</a>
          </td>
        </tr>
        {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
  <div class="card">
    <div class="card-header">
      <h3>Journals</h3>
    </div>
    <div class="card-body table-responsive">
      <table class="table table-hover table-striped">
        <thead>
        <tr>
          <th>Action</th>
          <th>Date</th>
          <th>User</th>
        </tr>
        </thead>
        <tbody>
        {% for journal in journals %}
        <tr>
          <td>{{ journal.action }}</td>
          <td>{{ journal.submitted_date|format_datetime() }}</td>
          <td>{{ journal.submitted_by.username }}</td>
        </tr>
        {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
  <div class="card">
    <div class="card-header">
      <h3>Observations</h3>
    </div>
    <div class="card-body">
      {% if observations %}
      {# TODO: Make this dataTables-enabled for sorting, filtering, etc. See Account Activity for an example. #}
      <div class="table-responsive p-0">
        <table class="table table-hover table-striped">
          <thead>
            <tr>
              <th>Created</th>
              <th>Kind</th>
              <th>Reporter</th>
              <th>Summary</th>
            </tr>
          </thead>
          <tbody>
            {% for observation in observations %}
            <tr>
              <td>{{ observation.created }}</td>
              <td>{{ observation.kind_display }}</td>
              <td>
                {# TODO: This won't work yet for non-User Observers #}
                <a href="{{ request.route_path('admin.user.detail', username=observation.observer.parent.username) }}">{{ observation.observer.parent.username }}</a>
              </td>
              <td>{{ observation.summary }}</td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {% else %}
      No observations.
      {% endif %}
    </div>
    <div class="card-footer">
      <button type="button" class="btn btn-outline-primary float-right" data-toggle="modal" data-target="#addObservationModal">
        Add Observation
      </button>
      <div class="modal fade" id="addObservationModal" tabindex="-1" role="dialog">
        <form method="POST" action="{{ request.route_path('admin.project.release.add_release_observation', project_name=release.project.normalized_name, version=release.version) }}">
          <input name="csrf_token" type="hidden" value="{{ request.session.get_csrf_token() }}">
          <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h4 class="modal-title" id="exampleModalLabel">Add Observation for {{ release.project.normalized_name }}</h4>
                <button type="button" class="close" data-dismiss="modal">
                  <span>&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <div class="row">
                  <div class="col-auto">
                    <div class="form-group">
                      {% for observation_kind in observation_kinds %}
                      <div class="form-check">
                        <input class="form-check-input" type="radio" name="kind" id="{{ observation_kind.value[0] }}" value="{{ observation_kind.value[0] }}">
                        <label class="form-check-label" for="{{ observation_kind.value[0] }}" >
                          {{ observation_kind.value[1] }}
                        </label>
                      </div>
                      {% endfor %}
                    </div>
                  </div>
                  <div class="col">
                    <div class="form-group">
                      <label class="form-check-label" for="projectObservationText">What do we need to know?</label>
                      <textarea class="form-control" id="projectObservationText" name="summary" rows="3" placeholder="Enter ..."></textarea>
                    </div>
                  </div>
                </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-info">Submit</button>
              </div>
            </div>
          </div>
        </form>
      </div> <!-- .modal #addObservationModal -->
    </div> <!-- .card-footer -->
  </div> <!-- .card #project_observations -->

  <div class="card">
    <div class="card-header">
      <h3>Metadata</h3>
    </div>
    <div class="card-body table-responsive">
      <table class="table table-hover">
        <tr>
          <th>Field</th>
          <th>Value</th>
        </tr>
        <tr>
          <td>Name</td>
          <td><code>{{ release.project.name }}</code></td>
        </tr>
        <tr>
          <td>Version</td>
          <td><code>{{ release.version }}</code></td>
        </tr>
        <tr>
          <td>Platform</td>
          <td><code>{{ release.platform }}</code></td>
        </tr>
        <tr>
          <td>Summary</td>
          <td><code>{{ release.summary }}</code></td>
        </tr>
        <tr>
          <td>Keywords</td>
          <td><code>{{ release.keywords }}</code></td>
        </tr>
        <tr>
          <td>Home-page</td>
          <td><code>{{ release.home_page }}</code></td>
        </tr>
        <tr>
          <td>Download-URL</td>
          <td><code>{{ release.download_url }}</code></td>
        </tr>
        <tr>
          <td>Author</td>
          <td><code>{{ release.author }}</code></td>
        </tr>
        <tr>
          <td>Author-Email</td>
          <td><code>{{ release.author_email }}</code></td>
        </tr>
        <tr>
          <td>Maintainer</td>
          <td><code>{{ release.maintainer }}</code></td>
        </tr>
        <tr>
          <td>Maintainer-email</td>
          <td><code>{{ release.maintainer_email }}</code></td>
        </tr>
        <tr>
          <td>License</td>
          <td><code>{{ release.license }}</code></td>
        </tr>
        <tr>
          <td>License-Expression</td>
          <td><code>{{ release.license_expression }}</code></td>
        </tr>
        <tr>
          <td>License-Files</td>
          <td>
            {% if release.license_files is not none %}
            {% for license_file in release.license_files %}
            <code>{{ license_file }}</code><br>
            {% endfor %}
            {% endif %}
          </td>
        </tr>
        <tr>
          <td>Classifier</td>
          <td>
            {% for classifier in release.classifiers %}
            <code>{{ classifier }}</code><br>
            {% endfor %}
          </td>
        </tr>
        <tr>
          <td>Requires-Dist</td>
          <td><code>{{ release.requires_dist }}</code></td>
        </tr>
        <tr>
          <td>Provides-Dist</td>
          <td><code>{{ release.provides_dist}}</code></td>
        </tr>
        <tr>
          <td>Obsoletes-Dist</td>
          <td><code>{{ release.obsoletes_dist}}</code></td>
        </tr>
        <tr>
          <td>Requires-Python</td>
          <td><code>{{ release.requires_python }}</code></td>
        </tr>
        <tr>
          <td>Requires-External</td>
          <td><code>{{ release.requires_external }}</code></td>
        </tr>
        <tr>
          <td>Project-URL</td>
          <td>
            {% for label, url in release.project_urls.items() %}
            <code>{{ label }}, {{ url }}</code><br>
            {% endfor %}
          </td>
        </tr>
          <tr>
          <td>Provides-Extra</td>
          <td>
            {% if release.provides_extra is not none %}
            {% for extra in release.provides_extra %}
            <code>{{ extra }}</code><br>
            {% endfor %}
            {% endif %}
          </td>
        </tr>
          <tr>
          <td>Dynamic</td>
          <td>
            {% if release.dynamic is not none %}
            {% for dynamic in release.dynamic %}
            <code>{{ dynamic }}</code><br>
            {% endfor %}
            {% endif %}
          </td>
        </tr>
      </table>
    </div>
  </div>
  <div class="card">
    <div class="card-header">
      <h3>Description Rendering</h3>
    </div>
    <div class="card-body table-responsive">
      <table class="table table-hover">
        <tr>
          <td>Rendered By Version</td>
          <td><code>{{ release.description.rendered_by }}</code></td>
        </tr>
        <tr>
          <td>Detected Content-Type</td>
          <td>
            {% if release.description.content_type is none %}
            <code>-NULL-</code>
            {% elif release.description.content_type == "" %}
            <code>-BLANK-</code>
            {% else %}
            <code>{{ release.description.content_type }}</code>
            {% endif %}
          </td>
        </tr>
        <tr>
          <td>Raw Input</td>
          <td><code>{{ release.description.raw }}</code></td>
        </tr>
        <tr>
          <td>Stored Output</td>
          <td><code>{{ release.description.html }}</code></td>
        <tr>
          <td>Database ID</td>
          <td><code>{{ release.description.id }}</code></td>
        </tr>
      </table>
    </div>
    <div class="card-footer">
      <div class="float-right">
        <form method="GET" action="{{ request.route_path('admin.project.release.render', project_name=release.project.normalized_name, version=release.version) }}">
          <button type="submit" class="btn btn-primary">Re-render</button>
        </form>
      </div>
    </div>
  </div>
{% endblock content %}
